﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Kai Patrick
 * Datum: 26.04.2008
 * Zeit: 18:39
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */

using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace Gamadus.Core
{
	/// <summary>
	/// Description of ActionManager.
	/// </summary>
	public class ActionManager
	{
		List<ActionSet> actionSets = new List<ActionSet>();
		
		public IEnumerable<ActionSet> ActionSets
		{
			get
			{
				return actionSets;
			}
		}
		
		public ActionManager()
		{
//			ActionSet aset;
//			aset = new ActionSet("control");
//				aset.Groups.Add("Code");
//					var paramType = new Dictionary<string, Resources.ActionParamPrototype>();
//					paramType.Add("Code",new Resources.ActionParamPrototype(typeof(Resources.ActionParamString)));
//					aset.Actions.Add(new Resources.ActionPrototype("Execute Code", "Code","executecode", "Execute a piece of code", paramType));
//					
//					//for testing:
//					paramType = new Dictionary<string, Resources.ActionParamPrototype>();
//					paramType.Add("Code",new Resources.ActionParamPrototype(typeof(Resources.ActionParamString)));
//					aset.Actions.Add(new Resources.ActionPrototype("Other Action", "Code","", "Do {0}", paramType));
//			actionSets.Add(aset);
			LoadLib(@"data\core.gxl");
		}
		
		public void LoadLib(string file)
		{
			//TODO: load from zip
			var settings = new XmlReaderSettings();
			settings.ValidationType = ValidationType.Schema;
			string path = Path.Combine(Path.Combine(System.Windows.Forms.Application.StartupPath,"data"),"actionlib.xsd");
			if(!File.Exists(path))
				throw new FileNotFoundException();
			settings.Schemas.Add("", path);
			XDocument xml;
			using(FileStream stream = new FileStream(file, FileMode.Open))
			{
				var reader = XmlReader.Create(stream,settings);
				while (reader.Read()) { };
				stream.Seek(0, SeekOrigin.Begin);
				xml = XDocument.Load(new StreamReader(stream));
				stream.Close();
			}
			Console.WriteLine(xml);
			foreach(var element in xml.Element("gamadusActionLib").Elements("actionSet"))
			{
				ActionSet aset = new ActionSet((string)element.Attribute("name"));
				foreach(var eGroup in element.Elements("group"))
				{
					string groupName = (string)eGroup.Attribute("name");
					aset.Groups.Add(groupName);
					foreach(var eAction in eGroup.Elements("action"))
					{
						string image = (eAction.Attribute("image") != null) ? (string)eAction.Attribute("image") : "";
						var paramTypes = new Dictionary<string, Resources.ActionParamPrototype>();
						foreach(var param in eAction.Element("parameters").Elements())
						{
							switch(param.Name.ToString())
							{
								case "appliesTo":
									paramTypes.Add("Applies To",new Resources.ActionParamPrototype(typeof(Resources.ActionParamAppliesTo)));
									break;
								case "string":
									if(param.Attribute("default") == null)
										paramTypes.Add((string)param.Attribute("name"),
										               new  Resources.ActionParamPrototype(typeof(Resources.ActionParamString)));
									else
										paramTypes.Add((string)param.Attribute("name"),
										               new  Resources.ActionParamPrototype(typeof(Resources.ActionParamString),
										                                                   (string)param.Attribute("default")));
									break;
								default:
									throw new NotImplementedException("keine lust mehr");
							}
						}
						aset.Actions.Add(new Resources.ActionPrototype((string)eAction.Attribute("name"), groupName, image, eAction.Element("description").Value, paramTypes));
					}
				}
			}
			throw new NotImplementedException();
		}
		
		public Resources.ActionPrototype FindPrototypeByName(string name)
		{
			foreach(ActionSet aset in ActionSets)
			{
				foreach(Resources.ActionPrototype action in aset.Actions)
				{
					if(action.Name == name)
						return action;
				}
			}
			return null;
		}
	}
	
	/// <summary>
	/// Description of ActionSet.
	/// </summary>
	public class ActionSet
	{
		List<string> m_groups = new List<string>();
		public List<string> Groups
		{
			get
			{
				return m_groups;
			}
		}
		string m_name;
		public string Name
		{
			get
			{
				return m_name;
			}
		}
		List<Resources.ActionPrototype> m_actions = new List<Resources.ActionPrototype>();
		public List<Resources.ActionPrototype> Actions
		{
			get
			{
				return m_actions;
			}
		}
		
		public ActionSet(string name)
		{
			m_name = name;
		}
	}
}
